package com.ruoyi.web.controller.personnelMatters;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SequenceUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.personnelMatters.domain.PersonAssessment;
import com.ruoyi.personnelMatters.service.IPersonAssessmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人事考核Controller
 *
 * @author ruoyi
 * @date 2022-09-16
 */
@RestController
@RequestMapping("/personnelMatters/personnelMatters")
public class PersonAssessmentController extends BaseController {
    @Autowired
    private IPersonAssessmentService personAssessmentService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询人事考核列表
     */
    @PreAuthorize("@ss.hasPermi('personnelMatters:personnelMatters:list')")
    @GetMapping("/list")
    public TableDataInfo list(PersonAssessment personAssessment) {
        startPage();
        List<PersonAssessment> list = personAssessmentService.selectPersonAssessmentList(personAssessment);
        return getDataTable(list);
    }

    /**
     * 导出人事考核列表
     */
    @PreAuthorize("@ss.hasPermi('personnelMatters:personnelMatters:export')")
    @Log(title = "人事考核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PersonAssessment personAssessment) {
        List<PersonAssessment> list = personAssessmentService.selectPersonAssessmentList(personAssessment);
        ExcelUtil<PersonAssessment> util = new ExcelUtil<PersonAssessment>(PersonAssessment.class);
        util.exportExcel(response, list, "人事考核数据");
    }

    /**
     * 获取人事考核详细信息
     */
    @PreAuthorize("@ss.hasPermi('personnelMatters:personnelMatters:query')")
    @GetMapping(value = "/{personAssessmentId}")
    public AjaxResult getInfo(@PathVariable("personAssessmentId") Long personAssessmentId) {
        return AjaxResult.success(personAssessmentService.selectPersonAssessmentByPersonAssessmentId(personAssessmentId));
    }


    /**
     * 审批人事考核
     */
    @PreAuthorize("@ss.hasPermi('personnelMatters:personnelMatters:edit')")
    @Log(title = "人事考核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PersonAssessment personAssessment) {
        if (personAssessmentService.selectPersonAssessmentByPersonAssessmentId(personAssessment.getPersonAssessmentId()).getAssessmentResult().equals("0")) {
            return toAjax(personAssessmentService.updatePersonAssessment(personAssessment));
        } else {
            return AjaxResult.error("不可二次审核");
        }
    }


    /**
     * 生成当月人事考核
     */
    @Log(title = "生成当月人事考核", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('personnelMatters:personnelMatters:generateOfMonth')")
    @GetMapping("/generateAppraisalForm")
    public AjaxResult generateAppraisalForm() {
        //从redis读取是否生成过当月考核
        SimpleDateFormat date = new SimpleDateFormat("yyyyMM");
        String perAssessment = "sys_perAssessment:" + date.format(new Date());
        //已生成过不再生成
        if (redisTemplate.opsForValue().get(perAssessment) != null) {
            return AjaxResult.error("当月已生成考核");
        }
        //未生成查表生成
        else {
            int num = personAssessmentService.generateAppraisalForm();
            redisTemplate.opsForValue().set(perAssessment, 1);
            redisTemplate.expireAt(perAssessment, SequenceUtil.getFirstDayOfNext(new Date()));
            return AjaxResult.success("共生成：" + num + "条记录");
        }
    }

    /**
     * 查询个人考核列表
     */
    @PreAuthorize("@ss.hasPermi('personnelMatters:ordinaryStaff:list')")
    @GetMapping("/ordinaryStaffList")
    public TableDataInfo ordinaryStaffList(PersonAssessment personAssessment) {
        startPage();
        personAssessment.setUserId(getUserId());
        List<PersonAssessment> list = personAssessmentService.selectPersonAssessmentList(personAssessment);
        return getDataTable(list);
    }

    /**
     * 获取个人考核详细信息
     */
    @PreAuthorize("@ss.hasPermi('personnelMatters:ordinaryStaff:query')")
    @GetMapping(value = "/ordinaryStaff/{personAssessmentId}")
    public AjaxResult ordinaryStaffGetInfo(@PathVariable("personAssessmentId") Long personAssessmentId) {
        return AjaxResult.success(personAssessmentService.selectPersonAssessmentByPersonAssessmentIdAndUid(personAssessmentId, getUserId()));
    }

}
